var information_type = null;
var voltage_type = null;
var buy_type = null;
$(function () {
    $.when(getdatadictionary("information_type"), getdatadictionary("voltage_type"), getdatadictionary("buy_type")).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'api/bdevice/list',
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                {  hidden: true,
                    label: '信息库类别 ', name: 'dInformationType', index: 'd_information_type', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in information_type) {
                            if (cellvalue == information_type[d].dKey) {
                                names = information_type[d].dValue;
                            }
                        }


                        return names;
                    }
                },
                {label: '物质编码', name: 'dCode', index: 'd_code', width: 80},
                {  hidden: true,label: '设备大类', name: 'dBigtype', index: 'd_bigtype', width: 80},
                {label: '设备中类', name: 'dMidtype', index: 'd_midtype', width: 80},
                {  hidden: true,label: '设备标签', name: 'dLabel', index: 'd_label', width: 80},
                {label: '设备名称', name: 'dName', index: 'd_name', width: 400},
                {  hidden: true,label: '主要技术参数', name: 'dParameter', index: 'd_parameter', width: 80},
                {
                    label: '电压等级 ', name: 'dVoltage', index: 'd_voltage', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in voltage_type) {
                            if (cellvalue == voltage_type[d].dKey) {
                                names = voltage_type[d].dValue;
                            }
                        }


                        return names;
                    }
                },
                {label: '查询预算参数名称', name: 'dBParameter', index: 'd_b_parameter', width: 80},
                {label: '查询预算值', name: 'dQuery', index: 'd_query', width: 80},
                {  hidden: true,
                    label: '采购级别 ', name: 'dBuyType', index: 'd_buy_type', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in buy_type) {
                            if (cellvalue == buy_type[d].dKey) {
                                names = buy_type[d].dValue;
                            }
                        }


                        return names;
                    }
                },
                {label: '购置单位', name: 'dUnit', index: 'd_unit', width: 80},
                {label: '购置单价', name: 'dPrice', index: 'd_price', width: 80},
                {  hidden: true, label: '单重（kg）', name: 'dWeight', index: 'd_weight', width: 80},
                {label: '是否主材', name: 'dIsmain', index: 'd_ismain', width: 80},

                {label: '配置预算项', name: 'dIshave', index: 'd_ishave', width: 80},

                {label: '预算项目分类', name: 'dClass', index: 'd_class', width: 80},
                {label: '预算项目', name: 'dItem', index: 'd_item', width: 80},
                {
                    label: '操作', name: '', width: 150, formatter: function (cellvalue, options, rowObject) {

                    return "<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\"" +
                        ",\"" + rowObject.dUnit + "\"" +
                        ",\"" + rowObject.dClass + "\"" +
                        ",\"" + rowObject.dItem + "\"" +
                        ",\"" + rowObject.dQuery + "\",1)' >" +
                        "<i class='fa  fa-align-justify'></i>&nbsp;设备工程量</a>"

                }
                }
            ],
            viewrecords: true,
            height: "100%",
            rowNum: 10,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    )
});
function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "information_type") {
            information_type = r.returnlist;
        }
        if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        }
        if (sss == "buy_type") {
            buy_type = r.returnlist;
        }


        vm.voltage_type = voltage_type;

    });
}

function show(guid, dUnit, dClass, dItem, dQuery, i) {
    var title = "设备工程量";
    var content = "bdevicequantities.html?dguid=" + guid + "&dUnit=" + dUnit + "&dClass=" + dClass + "&dItem=" + dItem + "&dQuery=" + dQuery;

    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['90%', '90%'],
        content: content

    });

}
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bDevice: {},
        voltage_type: voltage_type,
        buy_type: buy_type,
        information_type: information_type,
        status: ["是", "否"],
        q: {
            dVoltage: null,
            dLabel: null,
            mIsmain: null,
            mIshave: null,
            dName: null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bDevice = {};
            vm.voltage_type = voltage_type;
            vm.buy_type = buy_type;
            vm.information_type = information_type;
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.voltage_type = voltage_type;
            vm.buy_type = buy_type;
            vm.information_type = information_type;
            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bDevice.guid == null ? "api/bdevice/save" : "api/bdevice/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bDevice),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？并且同时删除设备对应的工程量？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bdevice/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bdevice/info/" + guid, function (r) {
                vm.bDevice = r.bDevice;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'dVoltage': vm.q.dVoltage,
                    'dLabel': vm.q.dLabel,
                    'dIsmain': vm.q.dIsmain,
                    'dIshave': vm.q.dIshave,
                    'dName': vm.q.dName
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});